<?php

declare(strict_types=1);

namespace Dreamcat\Class2Array\Cases;

use DreamCat\Array2Class\Utils\FeatureJudge;
use Dreamcat\Class2Array\DemoClass\DemoEnum;
use Dreamcat\Class2Array\DemoClass\IntEnum;
use Dreamcat\Class2Array\DemoClass\StringEnum;
use Dreamcat\Class2Array\DemoClass\WithEnum;
use Dreamcat\Class2Array\Impl\Class2Json;
use Dreamcat\Class2Array\Utils\EnumHelper;
use PHPUnit\Framework\TestCase;

/**
 * 枚举测试
 * @author vijay
 */
class EnumTest extends TestCase
{
    public function testEnum()
    {
        if (!FeatureJudge::hasEnum()) {
            # 没有枚举不用测试
            self::assertFalse(EnumHelper::isEnumValue(""));
            return;
        }
        self::assertTrue(EnumHelper::isEnumValue(IntEnum::ONE));
        self::assertFalse(EnumHelper::isEnumValue(IntEnum::ONE->value));

        $obj = new WithEnum(DemoEnum::Demo1, IntEnum::TOW, StringEnum::STR2);
        $expected = [
            "v1" => DemoEnum::Demo1->name,
            "v2" => IntEnum::TOW->value,
            "v3" => StringEnum::STR2->value,
        ];
        $encoder = new Class2Json();
        $actual = $encoder->jsonEncode($obj);
        self::assertEquals($expected, json_decode($actual, true));
    }
}

# end of file
